package com.pilipili.zeus.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pilipili.commonutils.Result;
import com.pilipili.zeus.eduService.entity.EduTeacher;
import com.pilipili.zeus.eduService.entity.vo.Query;
import com.pilipili.zeus.eduService.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 *
 * 作者 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-21
 */

@RestController
@RequestMapping("/eduService/edu")
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "查询所有用户")
    @GetMapping("findAll")
    public Result findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }

    @ApiOperation(value = "根据Id删除")
    @DeleteMapping("{id}")
    public Result remove(@PathVariable String id){
        boolean removeResult = teacherService.removeById(id);
        return removeResult?Result.ok():Result.error();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("pageFind/{current}/{limit}")
    public Result pageList(@PathVariable long current,
                                  @PathVariable long limit){
        Page<EduTeacher> page=new Page<>(current,limit);
        teacherService.page(page,null);
        long total = page.getTotal();
        List<EduTeacher> list = page.getRecords();
        return Result.ok().data("total",total).data("list",list);
    }

    @ApiOperation(value = "条件+分页查询")
    @PostMapping("pageCondition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) Query query){
        Page<EduTeacher> page=new Page<>(current,limit);
        teacherService.pageQuery(page, query);
        List<EduTeacher> list = page.getRecords();
        long total = page.getTotal();
        return  Result.ok().data("total", total).data("list",list);
    }

    @ApiOperation(value = "添加数据")
    @PostMapping("add")
    public Result add(@RequestBody EduTeacher teacher){
        boolean save = teacherService.save(teacher);
        return  save?Result.ok():Result.error();
    }

    @ApiOperation(value = "根据Id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return teacher!=null?Result.ok().data("teacher",teacher):Result.error();
    }

    @ApiOperation(value = "读取TeacherExcel中的表格,写入数据库")
    @PostMapping("addTeachers")
    public Result addTeachers(MultipartFile file){
        teacherService.saveTeachersDataByExcel(file,teacherService);
        return Result.ok();
    }
}

